// 业务统计
import adminLogsService from '@/services/adminlogs/index';

// 定义操作系统占比
async function loadCharts(time=100) {
  try {

    var serverReponse = await adminLogsService.stateLogsMethodversion(time);
    var methodData = serverReponse.data;
    methodData.sort((a,b)=>b.maxtime-a.maxtime);
    var mehtodNameArr = methodData.map(a=>a.name);
    var valueArr = methodData.map(a=>a.value);
    var timesArr = methodData.map(a=>a.maxtime);
    var option = {
      title: {
        text: '',
        subtext: ''
      },
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['次数', '最大耗时']
      },
      toolbox: {
        show: true,
        feature: {
          dataView: { show: true, readOnly: false },
          magicType: { show: true, type: ['line', 'bar'] },
          restore: { show: true },
          saveAsImage: { show: true }
        }
      },
      calculable: true,
      xAxis: [
        {
          type: 'category',
          // prettier-ignore
          data: mehtodNameArr
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          name: '次数',
          type: 'bar',
          data: valueArr,
          markPoint: {
            data: [
              { type: 'max', name: 'Max' },
              { type: 'min', name: 'Min' }
            ]
          },
          markLine: {
            data: [{ type: 'average', name: 'Avg' }]
          }
        },
        {
          name: '耗时',
          type: 'bar',
          data:timesArr,
          markPoint: {
            data: [
              { name: 'Max', value: 182.2, xAxis: 7, yAxis: 183 },
              { name: 'Min', value: 2.3, xAxis: 11, yAxis: 3 }
            ]
          },
          markLine: {
            data: [{ type: 'average', name: 'Avg' }]
          }
        }
      ]
    };
    return Promise.resolve(option);
  } catch (e) {
    return Promise.reject(e);
  }
}

export default loadCharts;
